package cn.itcast.flink.source;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.ParallelSourceFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Author itcast
 * Date 2022/1/11 15:22
 * Desc 通过用户自定义的方式生成订单数据
 * 订单数据的规则： 订单id：uuid 用户id:0~3数字 money:0~100之间的数字 时间:当前处理时间
 * 开发步骤：
 * 1.获取流执行环境
 * 2.设置并行度及其参数
 * 3.添加自定义数据源
 * 3.1. 实现run方法用于实现自定义的订单数据
 * 3.2. 自定义订单类 class Order
 * 4.将数据打印输出
 * 5.执行流环境
 *
 * 并行度设置四种方法：
 * 1.配置文件中，级别最低，全局设置
 * 2.Client 提交 Job，设置 flink run -p 2
 * 3.在全局的执行环境设置并行度
 * 4.在算子上设置并行度，级别最高
 */
public class OrderSource {
    public static void main(String[] args) throws Exception {
        //1.获取流执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //2.设置并行度及其参数
        env.setParallelism(1);
        //3.添加自定义数据源
        DataStreamSource<Order> source = env.addSource(new ParallelSourceFunction<Order>() {
            boolean isRunning = true;
            Random rm = new Random();
            //时间格式化工具
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            /**
             * 此方法主要用于实现自定义 source
             * @param ctx
             * @throws Exception
             */
            @Override
            public void run(SourceContext<Order> ctx) throws Exception {
                while (isRunning) {
                    ctx.collect(
                            new Order(
                                    UUID.randomUUID().toString(),
                                    String.valueOf(rm.nextInt(3)),
                                    rm.nextInt(101),
                                    sdf.format(System.currentTimeMillis())
                            )
                    );
                    TimeUnit.SECONDS.sleep(1);
                }
            }

            /**
             * 用于当前 source 被取消时候功能
             */
            @Override
            public void cancel() {
                isRunning = false;
            }
        }).setParallelism(2);
        // 3.1. 实现run方法用于实现自定义的订单数据
        // 3.2. 自定义订单类 class Order
        //4.将数据打印输出
        source.printToErr();
        //5.执行流环境
        env.execute();
    }

    /**
     * pojo 订单的对象
     */
    public static class Order {
        //订单id：uuid 用户id:0~3数字 money:0~100之间的数字 时间:当前处理时间
        private String oid;
        //用户
        private String uid;
        //money
        private int money;
        //处理时间
        private String processTime;

        public Order(String oid, String uid, int money, String processTime) {
            this.oid = oid;
            this.uid = uid;
            this.money = money;
            this.processTime = processTime;
        }

        public String getOid() {
            return oid;
        }

        public void setOid(String oid) {
            this.oid = oid;
        }

        public String getUid() {
            return uid;
        }

        public void setUid(String uid) {
            this.uid = uid;
        }

        public int getMoney() {
            return money;
        }

        public void setMoney(int money) {
            this.money = money;
        }

        public String getProcessTime() {
            return processTime;
        }

        public void setProcessTime(String processTime) {
            this.processTime = processTime;
        }

        @Override
        public String toString() {
            return "Order{" +
                    "oid='" + oid + '\'' +
                    ", uid='" + uid + '\'' +
                    ", money=" + money +
                    ", processTime='" + processTime + '\'' +
                    '}';
        }
    }
}
